package com.tj.collection.db.dao.impl;

import com.fqgj.common.api.Page;
import com.fqgj.common.base.AbstractBaseMapper;
import com.tj.collection.db.dao.ClOutboundDetailDao;
import com.tj.collection.db.entity.ClOutboundDetailEntity;
import com.tj.collection.db.entity.business.ClOutboundInfoEntity;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: Karl
 * Date: 2020-02-25
 * Time: 17:10:20.149
 */

@Repository
public class ClOutboundDetailDaoImpl extends AbstractBaseMapper<ClOutboundDetailEntity> implements ClOutboundDetailDao {

    @Override
    public List<ClOutboundDetailEntity> selectClOutboundDetailListByParams(Map<String, Object> params) {
        if (!params.containsKey("deleted")) {
            params.put("deleted", 0);
        }
        return getSqlSession().selectList(this.getStatement("selectByParams"), params);
    }

    @Override
    public ClOutboundDetailEntity selectClOutboundDetailByMobile(String batchNo, String mobile, String tradeNo) {
        if (batchNo == null || batchNo.length() == 0 || mobile == null || mobile.length() == 0) {
            return null;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("batchNo", batchNo);
        params.put("mobile", mobile);
        params.put("tradeNo", tradeNo);
        List<ClOutboundDetailEntity> list = this.selectClOutboundDetailListByParams(params);
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public ClOutboundDetailEntity selectClOutboundDetailByMobile(String batchNo, String mobile) {
        if (batchNo == null || batchNo.length() == 0 || mobile == null || mobile.length() == 0) {
            return null;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("batchNo", batchNo);
        params.put("mobile", mobile);
        List<ClOutboundDetailEntity> list = this.selectClOutboundDetailListByParams(params);
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public List<ClOutboundInfoEntity> selectClOutboundInfoListByParams(Map<String, Object> params) {
        if (!params.containsKey("deleted")) {
            params.put("deleted", 0);
        }
        if (params.containsKey("page")) {
            Page page = (Page) params.get("page");
            page.setTotalCount(this.countClOutboundInfoListByParams(params));
        }
        return getSqlSession().selectList(this.getStatement("selectClOutboundInfoListByParams"), params);
    }

    private int countClOutboundInfoListByParams(Map<String, Object> params) {
        return getSqlSession().selectOne(this.getStatement("countClOutboundInfoListByParams"), params);
    }
}